﻿using System;
using System.Linq;
using ITM495.OOExamples.Core;
using ITM495.OOExamples.Core.Logger;

namespace ITM495.OOExamples
{
    class Program
    {
        static Options Options { get; set; }
        static ControllerFactory ControllerFactory { get; set; }

        static Program()
        {
            ControllerFactory = new ControllerFactory();
        }

        /// <summary>
        /// This is the main entry point to the example project.
        /// 
        /// This responsibility of this class is to parse the command line arguements, create the correct IExampleRunner instance and the run the correct example
        /// </summary>
        static void Main(string[] args)
        {
            Console.WriteLine("ITM495.OOExamples project ....\n\n");
            
            //
            // Parse our arguments and setup our logging
            //
            InitializeOptions(args);
            InitializeLogging();

            //
            // Create a new instance of the requested controller
            //
            var runner = ControllerFactory.GetController(Options.ControllerName, Options);
            runner.Run(Options);


            //
            // Thanks for playing
            //
            Console.WriteLine("\n\nProgram complete. Press any key to exit.....");
            Console.ReadKey();
        }

        /// <summary>
        /// Initializes the Options property using the given arguments strgin
        /// </summary>
        public static void InitializeOptions(string[] args)
        {
            Options = new Options(args);
        }

        /// <summary>
        /// This method initializes the LogFactory by looking for the '-l' option.
        /// 
        /// If no logger is found ConsoleLogger is used
        /// </summary>
        private static void InitializeLogging()
        {
            var instance = Options.Keys.Contains("-l")  
                                ? LogManager.CreateLogger(Options["-l"]) 
                                : new ConsoleLogger();

            LogManager.Initialize(instance);
        }
    }
}
